var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');
var user = require('../../../services/user.js');

var app = getApp();

Page({
  data: {
    userInfo: {},
    applySuccess: false,
    myShopName: "", //店铺名称
    myShopType: 1, //默认为批发店
    shopTypes: [
      { name: '1', value: '批发店', checked: 'true' },
      { name: '2', value: '私密型零售店' },
      { name: '3', value: '公开型零售店' }
    ],
    adminName: "" //管理员账号
  },
  radioChange: function (e) {
    this.setData({
      myShopType: e.detail.value
    });
  },
  myShopNameInput: function(e){
    this.setData({ myShopName: e.detail.value })
  },
  submitApplyOpenShop: function() {
    let that = this;
    if (!that.data.myShopName) {
      util.showErrorToast('店铺名称不能为空，请填写！');
      return false;
    }
    wx.showLoading({
      title: '正在提交申请...',
      mask: true,
      success: function () {
      }
    });

    //申请开店
    util.request(api.ApplyOpenShop, { myShopName: that.data.myShopName, myShopType: that.data.myShopType }).then(function (res) {
      if (res.errno === 0) {
        wx.hideLoading();
        that.setData({
          applySuccess: true,
          adminName: res.data.sys_user_name
        });
      } 
      else {
        util.showErrorToast(res.errmsg);
      }
    })
  },
  returnTo: function() {
    wx.switchTab({
      url: "/pages/ucenter/index/index"
    })
  },
  onLoad: function (options) {


    wx.setNavigationBarTitle({
      title: "个人中心 - 我要开店"
    });
    //检测是否已开店
    let that = this;
    util.request(api.QueryOpenShop).then(function (res) {
      if (res.errno === 0) {
        that.setData({
          applySuccess: true,
          myShopName: res.data.root_dept_name,
          adminName: res.data.sys_user_name
        })
      }
      else {
        that.setData({
          applySuccess: false
        })
      }
    })
  },
  onShow: function () {
  }
})